*-------------------------------------------------------------------------------------------------------------
*
*	DETRENDING - Hodrick-Prescott
*	(spell-wise detrending given missing values)
*
*-------------------------------------------------------------------------------------------------------------
*================= SETTINGS ========================================================================================

local country ccode /* country-indicator: starting from 1, steps of 1 */
local time year

local onesided 0 /* one- or two-sided filtering:
					0 - two-sided
					1 - one-sided */
					
local absolute 1 /*0 - percentage deviations from trend
				   1 - absolute deviations from trend */				

local hp 1 /* Hodrick-Prescott
			  0 - no
			  1 - yes */
	local smooth 6.25 /* smoothing-factor:
						 monthly optimal - 129,600
						 quarterly optimal - 1600
						 annualy optimal - 6.25 */

local minobs = 5 // minimum number of observations necessary for detrending a spell
	
*=================================================================================================================

foreach v of local det { // variable-loop
	n display "Detrending `v'"
	
	* SPELLS
	tsspell, cond(`v'!=.) end(`v'_pend) seq(`v'_pseq) spell(`v'_pspell) /*find spells*/
	egen hmseq_`v' = max(`v'_pseq), by(`country' `v'_pspell) /*determine length of each spell*/
	tsspell, cond(`v'!=. & hmseq_`v'>=3) end(`v'_hpend) seq(`v'_hpseq) spell(`v'_hpspell) /*neglect spells too short for detrending*/
	egen mhp_`v' = max(`v'_hpspell), by(`country') /*find max. number of spells per country*/
	egen mseq_`v' = max(`v'_hpseq), by(`country' `v'_hpspell) /*length of each spell*/
	
	* TREND & CYCLE
	gen cyc_`v' = .
	gen tr_`v' = .	
	//gen lin_cyc_`v' = .
	//gen lin_tr_`v' = .
	
	sum `country' /* get number of countries in sample */
	local cnumb `r(max)' 
	
	forvalues x = 1/`cnumb' 	{ /*country-loop*/
	

		*** number of spells by country
		sum mhp_`v' if `country' == `x'
		local max `r(max)'
		
		** Filter for each spell
		forvalues y = 1/`max'		 {	/*spell-loop*/
		
		
		
		*___________________________ HP-Filter _________________________________	
		
			if(`onesided'==1 & `hp'==1)		{ /* one-sided */
				*** length of spell
				sum mseq_`v' if (`country' == `x' & `v'_hpspell == `y')
				local length `r(max)'	
				
					** one-sided HP-Filtering (starting from 3 as HP-filter needs at least 3 consecutive obs)
					forvalues z = 3/`length' {	
					
						hprescott `v' if (`country' == `x' & `v'_hpspell == `y' & `v'_hpseq <= `z'), stub(hp_`x'_`y') smooth(`smooth')
		
						replace cyc_`v' = hp_`x'_`y'_`v'_1 if (`country' == `x' & `v'_hpspell == `y' & `v'_hpseq == `z')
						replace tr_`v' = hp_`x'_`y'_`v'_sm_1 if (`country' == `x' & `v'_hpspell == `y' & `v'_hpseq == `z')
		
						drop hp_`x'_`y'_`v'_1 hp_`x'_`y'_`v'_sm_1 
						
											}
										}
			else {
										}
			
			sum mseq_`v' if (`country' == `x' & `v'_hpspell == `y')	
			if(`onesided'==0 & `hp'==1 & `r(N)'>=`minobs') { /* two-sided */

						hprescott `v' if (`country' == `x' & `v'_hpspell == `y'), stub(hp_`x'_`y') smooth(`smooth')
		
						replace cyc_`v' = hp_`x'_`y'_`v'_1 if (`country' == `x' & `v'_hpspell == `y')
						replace tr_`v' = hp_`x'_`y'_`v'_sm_1 if (`country' == `x' & `v'_hpspell == `y')
		
						drop hp_`x'_`y'_`v'_1 hp_`x'_`y'_`v'_sm_1 			
			
										}
			else {
										}
										
									} // spell
							} // country
										

	/*
	order `v' `v'_hpspell mhp_`v' `v'_hpseq mseq_`v' cyc_`v' tr_`v'
	*/
	
	* DEVIATIONS
	if `: list v in irates'{ /*ppts - deviations for interest rate variables*/ 
		** HP-Filter
		gen `v'_hp = cyc_`v'
		drop cyc_`v'
		}
	else {
		if `absolute'==1 { /* absolute deviations for other level variables*/
		** Linear
		** HP-Filter
		gen `v'_hp = cyc_`v'
		drop cyc_`v'
		}	
	if `absolute'==0{ /* percentage deviations from trend*/
		** HP-Filter
		gen `v'_hp = cyc_`v'/tr_`v'*100 if(tr_`v'>0)
		drop cyc_`v' 
		}
	}
		
	** drop helvars
	drop *_pend *_hpend *_pseq *_hpseq *_hpspell *_pspell hmseq* mhp* mseq* 
}


